package icu.twtool.ui.component

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.onClick
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import icu.twtool.ui.style.MyColors
import icu.twtool.ui.style.MyCursor
import icu.twtool.ui.style.MyShape

/**
 * @author Wen
 * @since 2022-11-05
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
@Suppress("FunctionName")
fun <T> PagingBar(state: PagingState<T>, modifier: Modifier = Modifier, onChange: () -> Unit) {
    Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) {
        val leftIcon = painterResource("icons/left.svg")
        val leftLeftIcon = painterResource("icons/left-left.svg")
        val rightIcon = painterResource("icons/right.svg")
        val rightRightIcon = painterResource("icons/right-right.svg")
        val iconModifier = Modifier.clip(MyShape.RoundedCornerSmall).pointerHoverIcon(MyCursor.HandCursor).background(
            MyColors.PrimaryBackground
        ).padding(4.dp)
        Icon(leftLeftIcon, null, tint = MyColors.OnPrimaryBackground, modifier = iconModifier.onClick {
            if (state.page != state.min) {
                state.page= state.min
                onChange()
            }
        })
        Spacer(Modifier.requiredWidth(8.dp))
        Icon(leftIcon, null, tint = MyColors.OnPrimaryBackground, modifier = iconModifier.onClick {
            if (state.page > state.min) {
                state.page--
                onChange()
            }
        })
        Spacer(Modifier.requiredWidth(8.dp))
        Text("${state.page} / ${state.max}")
        Spacer(Modifier.requiredWidth(8.dp))
        Icon(rightIcon, null, tint = MyColors.OnPrimaryBackground, modifier = iconModifier.onClick {
            if (state.page < state.max) {
                state.page++
                onChange()
            }
        })
        Spacer(Modifier.requiredWidth(8.dp))
        Icon(rightRightIcon, null, tint = MyColors.OnPrimaryBackground, modifier = iconModifier.onClick {
            if (state.page != state.max) {
                state.page = state.max
                onChange()
            }
        })
    }
}

/**
 * 分页状态
 */
class PagingState<T>(page: Int, max: Int, min: Int, size: Int) {

    var data: List<T> by mutableStateOf(listOf())

    var page: Int by mutableStateOf(page)

    var max: Int by mutableStateOf(max)

    val min: Int by mutableStateOf(min)

    val size: Int by mutableStateOf(size)
}

@Composable
@Suppress("FunctionName")
fun <T> rememberPagingState(page: Int, max: Int, min: Int = 1, size: Int = 10) = remember { PagingState<T>(page, max, min, size) }